//
//  AirGraphicsRender.h
//  AirCpp
//
//  Created by Penghao on 14-2-7.
//  Copyright (c) 2014年 PengHao.

//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

#ifndef __AirCpp__AirGraphicsRender__
#define __AirCpp__AirGraphicsRender__

#include <iostream>
#include "../AirContainer/AirArray.h"
#include "../AirObject/AirObject.h"
#include "../AirMath/AirMath.h"
#include "AirGraphicsUtility.h"

namespace AirCpp{
    using namespace AirMath::LinearAlgebra;
    typedef AirPoint3 AirCoordinate3D;
    typedef AirPoint3 AirPosition3D;
    typedef AirPoint3 AirVector3D;
    
    
    class AirGraphicsRender : public AirObject {
    protected:
        AirCoordinate3D *coordinate;
        AirPosition3D *center;
        bool needUpdate;
        AirMutableArray<AirGraphicsRender> render_array;
        GLfloat program;
        GLchar *vshfilename;
        GLchar *fshfilename;
    
    public:
        
        int init(AirCoordinate3D *coordinate_, AirPosition3D *center_, GLchar *vshfilename_, GLchar *fshfilename_){
            coordinate = retain(coordinate_);
            center = retain(center_);
            vshfilename = vshfilename_;
            fshfilename = fshfilename_;
            return 0;
        }
        
        void add_render(AirGraphicsRender *_render){
            render_array.add_object(_render);
        };
        
        void remove_render(AirGraphicsRender *_render){
            render_array.remove_object(_render);
        }
        
        virtual void bind_shader_atrribute(GLuint shaderProgram){
            
        }
        
        virtual void bind_buffer(void){
            
        }
        
    public:
        
        int compire_shader_program(){
            char *vsh_str, *fsh_str;
            vsh_str = AirGraphReadRenderFile(vshfilename);
            fsh_str = AirGraphReadRenderFile(fshfilename);
            if (!vsh_str||!fsh_str) {
                printf("file text is null");
                return -1;
            }
            program = AirGraphLoadProgram ( vsh_str, fsh_str );
            if (program == -1) {
                return -1;
            }
            free(vsh_str);
            free(fsh_str);
            bind_shader_atrribute(program);
            bind_buffer();
            return 0;
        }
        
        virtual ~AirGraphicsRender(void){
            release(coordinate);
            release(center);
        };
        
        virtual void update(bool forcibly)
        {
        }
        
        virtual void render()
        {
        }
    };
}
#endif /* defined(__AirCpp__AirGraphicsRender__) */
